use std::env::args;
use nix::libc::exit;
use std::process::Command;
use crate::utilty::{log_error, log_info};


pub fn tar() {
    let tar_path = args().nth(2).expect("no tar file path");
    let tar_path = tar_path.trim().to_string();
    let dist_path = args().nth(3).expect("no dist path");
    let dist_path = dist_path.trim().to_string();
    if tar_path.is_empty() || dist_path.is_empty() {
        log_error("args count error".to_string());
        return;
    }
    tar_image(tar_path, dist_path);
    log_info("tar success".to_string())
}
//ok
///对tar命令的封装
pub fn tar_image(tar_file: String,dist_dir:String) {
    let mut child = Command::new("tar")
        .arg("-xf")
        .arg(tar_file)
        .arg("-C")
        .arg(dist_dir)
        .spawn()
        .expect("tar exec failed");
    let status = child.wait().expect("tar wait failed");
    match status.success() {
        true => {
            log_info("tar exec succeeded".to_string())
        }
        false => {
            log_error("tar failed".to_string());
            unsafe { exit(3) }
        }
    }
}
